package com.codeying.langchain4j;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.codeying.entity.QuestionBank;
import com.codeying.service.QuestionBankService;
import com.codeying.service.UserService;
import com.codeying.utils.CommonUtils;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class AiTool {

    @Autowired
    QuestionBankService questionBankService;
    @Autowired
    UserService userService;

    @Tool("根据题目模糊查询题库信息")
    public String addReservation2(@P("用户ID") String userid, @P("题目名") String name) {
        List<QuestionBank> list = questionBankService.list(new LambdaQueryWrapper<QuestionBank>().like(QuestionBank::getName, name).eq(QuestionBank::getUserid, userid));
        StringBuilder s = new StringBuilder();
        for (QuestionBank questionBank : list) {
            s.append("{" +
                    ", 题目='" + questionBank.getName() + '\'' +
                    ", 题目内容='" + questionBank.getContent() + '\'' +
                    ", 答案='" + questionBank.getAnswer() + '\'' +
                    ", 知识点='" + questionBank.getLabels() + '\'' +
                    '}').append("\n");
        }
        return s.isEmpty() ? "没有结果" : s.toString();
    }

    @Tool("根据知识点模糊查询题库信息")
    public String wadaw(@P("用户ID") String userid, @P("知识点") String name) {
        List<QuestionBank> list = questionBankService.list(new LambdaQueryWrapper<QuestionBank>().like(QuestionBank::getLabels, name).eq(QuestionBank::getUserid, userid));
        StringBuilder s = new StringBuilder();
        for (QuestionBank questionBank : list) {
            s.append("{" +
                    ", 题目='" + questionBank.getName() + '\'' +
                    ", 题目内容='" + questionBank.getContent() + '\'' +
                    ", 答案='" + questionBank.getAnswer() + '\'' +
                    ", 知识点='" + questionBank.getLabels() + '\'' +
                    '}').append("\n");
        }
        return s.isEmpty() ? "没有结果" : s.toString();
    }

    //2.工具方法: 查询预约信息
    @Tool("保存题库信息")
    public Object findReservation(@P("用户ID") String userid, @P("题目名") String name, @P("题目内容") String content, @P("题目答案") String answer, @P("知识点") String labels) {
        QuestionBank q = new QuestionBank();
        q.setId(CommonUtils.newId());
        q.setUserid(userid);
        q.setName(name);
        q.setContent(content);
        q.setAnswer(answer);
        q.setLabels(labels);
        q.setCreatetime(new Date());
        boolean res = questionBankService.save(q);
        return res ? "提示用户保持成功" : "保持题目失败" ;
    }
}
